﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Data.SqlClient;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace NutriStat
{
    /// <summary>
    /// Allows the user to connect to a Microsoft SQL server database.
    /// </summary>    
    public partial class SQLConnectDialog : NutriDialog
    {
        private OpenTableDialogBase m_fOpenTable;

        /// <summary>
        /// Constructor
        /// </summary>        
        public SQLConnectDialog()
        {
            InitializeComponent();
        }

        /// <summary>
        /// Custom constructor
        /// </summary>
        /// <param name="openTable">Calling object</param>
        public SQLConnectDialog(OpenTableDialogBase openTable)
        {
            InitializeComponent();
            m_fOpenTable = openTable;
        }

        /// <summary>
        /// Destructor
        /// </summary>        
        ~SQLConnectDialog()
        {
            m_fOpenTable.UseWaitCursor = false;
            m_fOpenTable.Enabled = true;
        }

        private void SQLConnectDialog_Load(object sender, EventArgs e)
        {
            m_fOpenTable.Enabled = false;
            m_fOpenTable.UseWaitCursor = true;

            ConfigureFormControls();
        }

        private void buttonCancel_Click(object sender, EventArgs e)
        {
            m_fOpenTable.Enabled = true;
            m_fOpenTable.UseWaitCursor = false;
            this.Close();
        }

        private void buttonTest_Click(object sender, EventArgs e)
        {
            // Here's where we test the connection. If we make it through the TRY block
            // we can likely assume the connection is OK. This may need some cleanup
            // since I don't know if there's a case where we'd pass the TRY but still
            // not have a database to connect to.
            try
            {
                string server = textBoxServerName.Text;
                string database = textBoxDatabaseName.Text; 
                //SqlConnection conn = new SqlConnection("Server=" + server + ";Trusted_Connection=True;");            
                SqlConnection conn = new SqlConnection("Data Source=" + server + ";Initial Catalog=" + database + ";Trusted_Connection=True;");
                conn.Open();
                conn.Close();
                conn.Dispose();
                MessageBox.Show(SharedStrings.NOTICE_CONNECTION_OK);
            }
            catch
            {
                MessageBox.Show(SharedStrings.NOTICE_CONNECTION_FAILED);
            }
        }

        private void buttonOK_Click(object sender, EventArgs e)
        {
            try
            {
                string server = textBoxServerName.Text;
                string database = textBoxDatabaseName.Text;                
                SqlConnection conn = new SqlConnection("Data Source=" + server + ";Initial Catalog=" + database + ";Trusted_Connection=True;");
                conn.Open();
                conn.Close();
                conn.Dispose();
                //m_fOpenTable.ClearList();
                m_fOpenTable.ConnectionString = "Data Source=" + server + ";Initial Catalog=" + database + ";Trusted_Connection=True;";
                m_fOpenTable.PopulateSQL();
                m_fOpenTable.UseWaitCursor = false;
                m_fOpenTable.Enabled = true;                
                this.Close();
            }
            catch (Exception ex)
            {
                MessageBox.Show(SharedStrings.NOTICE_CONNECTION_FAILED + " " + ex.ToString());
            }
        }

        private void SQLConnectDialog_FormClosed(object sender, FormClosedEventArgs e)
        {
            m_fOpenTable.UseWaitCursor = false;
            m_fOpenTable.Enabled = true;
        }

        /// <summary>
        /// Configures the form controls based on the user's preferences and regional settings.
        /// </summary>
        protected internal override void ConfigureFormControls()
        {
            this.Text = SharedStrings.CONNECT_SQL;
            labelHeading.Text = SharedStrings.CONNECT_TO_A_SQL_DATABASE;
            labelServerName.Text = SharedStrings.SERVER_NAME;
            labelDatabaseName.Text = SharedStrings.DATABASE_NAME;
            groupBoxAuthentication.Text = SharedStrings.AUTHENTICATION;
            buttonOK.Text = SharedStrings.OK;
            buttonTest.Text = SharedStrings.TEST;
            buttonCancel.Text = SharedStrings.CANCEL;
        }
    }
}
